✅ Fix incorrect mocking in unit tests (issue #1780)#1781
Merged
tiangolo merged 2 commits intofastapi:masterfrom Jan 22, 2026
Merged
✅ Fix incorrect mocking in unit tests (issue #1780)#1781tiangolo merged 2 commits intofastapi:masterfrom
tiangolo merged 2 commits intofastapi:masterfrom
Conversation
YuriiMotov
approved these changes
Sep 3, 2025
Member
There was a problem hiding this comment.
LGTM
I suggest removing several lines to simplify tests (I tested it locally - it doesn't break tests).
Currently if you replace session.exec(select(1)) in this line with pass, this assertion will not fail as it's expected.
This PR fixes mocking the Session and after applying it, that test will not only ensure that there was no exception in init, but also that select(1) was actually called.
The same for https://github.com/fastapi/full-stack-fastapi-template/blob/master/backend/app/tests/scripts/test_test_pre_start.py
@vicaya, thank you!
Could you please take a look at my suggested changes? (done)
Resolve fastapi#1780
daa3017 to
5321f5b
Compare
Member
|
It seems we now have some conflicts here 🤔 😔 |
Contributor
|
This pull request has a merge conflict that needs to be resolved. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix issue #1780: the 2
test_init_successful_connectiontests were basically noops due to misuse of MagicMock.The errors went undetected until python 3.12+ tightened up the MagicMock implementation.